package com.sdp.th.netty.actor;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdp.core.bizc.actor.SysActorConfig;
import com.sdp.th.netty.utils.NettyUtils;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ActorManager {
	
	@Autowired
	private SysActorConfig sysActorConfig;

	/**
	 * 
	 * @param obj
	 * @param deviceNo
	 */
	public void tell(Object obj ,String deviceNo) {
		try {
			Map<String,ActorRef> sysActorMap = sysActorConfig.getSysActorMap();
			ActorSystem actorSystem = sysActorConfig.getSysActorSystem();
			String key = NettyUtils.ACTOR_FIX+deviceNo;
			ActorRef actor = sysActorMap.get(key);
		
			if(actor==null) {
				actor = actorSystem.actorOf(Props.create(BusinessActor.class), key);
				sysActorMap.put(key, actor);
			}
			actor.tell(obj, ActorRef.noSender());
		} catch (Exception e) {
			log.error("ActorManager执行出现异常：{}",NettyUtils.stackTraceToString(e.getClass().getName(), e.getMessage(), e.getStackTrace()));
		}	
	}
}
